package com.example.springbootdemo.sevice;

import com.example.springbootdemo.dto.TestRedisSetDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  redis接口
 * </p>
 *
 * @author yurenwei
 * @since 2023/9/5
 */
@Slf4j
@Service
public class RedisService {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 测试set方法
     *
     * @param params
     */
    public void testSet(@RequestBody TestRedisSetDTO params) {
        redisTemplate.opsForValue().set(params.getKey(),params.getValue());
    }

    /**
     * 测试get方法
     *
     * @param key
     * @return
     */
    public String testGet(String key) {
        return (String) redisTemplate.opsForValue().get(key);
    }

    /**
     * 测试key过期
     */
    public void testKeyExpiration() {
        String orderNo = "123456";
        // 设置5秒钟过期
        redisTemplate.opsForValue().set(orderNo,"订单编号",5, TimeUnit.SECONDS);
    }
}
